<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
        <el-form-item
          label="模板类型"
          v-if="
            props.modal.params.platform == 'WechatOfficialAccount' ||
            props.modal.params.platform == 'WechatMiniProgram'
          "
        >
          <el-radio-group v-model="form.model.type">
            <el-radio label="default">默认配置</el-radio>
            <el-radio label="custom">自定义配置</el-radio>
          </el-radio-group>
        </el-form-item>
        <div
          v-if="
            (form.model.type == 'default' &&
              props.modal.params.platform == 'WechatOfficialAccount') ||
            (form.model.type == 'default' && props.modal.params.platform == 'WechatMiniProgram')
          "
        >
          <el-form-item label="通知标题">
            <el-input
              v-model="form.model.name"
              placeholder="注册完成给上级发送"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="模板编号" v-if="props.modal.params.platform != 'WechatMiniProgram'">
            <el-input v-model="form.model.wechat.temp_no" placeholder="" disabled></el-input>
          </el-form-item>
          <el-form-item label="模板Id">
            <el-input v-model="form.model.content.template_id" disabled>
              <template v-if="checkAuth('admin.notification.config.gettemplateid')" #append>
                <span
                  class="cursor-pointer"
                  @click="getTemplateId('0')"
                  v-if="!form.model.content.template_id"
                  >立即获取</span
                >
                <el-popover
                  v-model:visible="refundPopover"
                  :width="250"
                  trigger="click"
                  v-if="form.model.content.template_id"
                >
                  <div class="sa-flex sa-m-b-16">
                    <el-icon class="question sa-m-r-8">
                      <QuestionFilled />
                    </el-icon>
                    <div>确定要重新获取模板Id吗？</div>
                  </div>
                  <div class="sa-flex">
                    <el-checkbox
                      v-model="isDelete"
                      label="删除旧模板"
                      class="sa-m-r-20"
                      true-label="1"
                      false-label="0"
                    />
                    <el-button size="small" @click="refundPopover = false">取消</el-button>
                    <el-button size="small" type="primary" @click="getTemplateId(isDelete)"
                      >确定</el-button
                    >
                  </div>
                  <template #reference>
                    <span>重新获取</span>
                  </template>
                </el-popover>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="模板" class="field">
            <div v-for="i in form.model.wechat.fields" :key="i" class="sa-flex">
              <div v-if="i.name">{{ i.name }}：</div>
              <div>{{ `{{${i.template_field}.DATA` }}}}</div>
            </div>
          </el-form-item>
        </div>
        <div v-if="form.model.type == 'custom' || props.modal.params.platform == 'Sms'">
          <el-form-item label="模板消息Id">
            <el-input
              v-model="form.model.content.template_id"
              placeholder="请输入模版消息Id"
            ></el-input>
          </el-form-item>
          <div
            v-for="(item, index) in form.model.content.fields"
            :key="item"
            class="sa-flex sa-m-b-24"
          >
            <div class="field-name sa-flex sa-row-right sa-p-r-12">
              <span v-if="item.field">{{ item.name }}</span>
              <el-input
                v-else
                v-model="item.name"
                placeholder="请输入名称"
                class="sa-w-100"
              ></el-input>
            </div>
            <el-input
              v-model="item.template_field"
              class="sa-w-168 sa-m-r-24"
              placeholder="请输入模版名称"
            ></el-input>
            <el-input v-model="item.value" class="sa-w-168" placeholder="请输入默认值"></el-input>
            <div
              v-if="!item.field"
              class="field-del sa-m-l-12 sa-flex cursor-pointer"
              @click.stop="fieldDel(index)"
            >
              删除
            </div>
          </div>
          <el-form-item>
            <el-button class="sa-m-l-12" icon="Plus" @click="addContent">添加</el-button>
          </el-form-item>
        </div>
        <div v-if="props.modal.params.platform == 'Email'">
          <div v-for="item in fieldList.data.fields" :key="item" class="sa-flex sa-m-b-24">
            <div class="field-name sa-flex sa-row-right sa-p-r-12">
              <span v-if="item.field">{{ item.name }}</span>
            </div>
            <el-input
              v-model="item.field"
              class="sa-w-168 sa-m-r-24"
              placeholder="请输入模版名称"
            ></el-input>
          </div>
          <div class="warning-title"> 请按照如下格式在文档中插入要显示的字段 p:{字段名} </div>
          <sa-editor v-model:content="form.model.content"></sa-editor>
        </div>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button v-auth="'admin.notification.config.edit'" type="primary" @click="confirm"
        >更新</el-button
      >
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import admin from '@/app/admin/api';
  import SaEditor from '@/sheep/components/sa-editor/sa-editor.vue';
  import { checkAuth } from '@/sheep/directives/auth';
  import { cloneDeep } from 'lodash';

  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  const value1 = ref(false);
  let formRef = ref(null);
  const form = reactive({
    model: {
      type: 'custom',
      name: '',
      wechat: {},
      content: '',
    },
    rules: {},
  });
  const fieldList = reactive({
    data: {},
  });
  const refundPopover = ref(false);
  const isDelete = ref('1');
  //删除
  function fieldDel(index) {
    form.model.content.fields.splice(index, 1);
  }
  function addContent() {
    form.model.content.fields.push({
      name: '',
      template_field: '',
      value: '',
    });
  }
  //获取模板id
  async function getTemplateId(e) {
    refundPopover.value = false;

    const { data } = await admin.notification.getTemplateId(
      props.modal.params.event,
      props.modal.params.platform,
      { is_delete: e, template_id: e == 1 ? form.model.content.template_id : '' },
    );
    form.model.content.template_id = data;
  }
  // 获取详情
  async function getDetail() {
    const { data } = await admin.notification.config(
      props.modal.params.event,
      props.modal.params.platform,
    );
    form.model = data;
    if (props.modal.params.platform == 'Email') {
      fieldList.data = data.content;
      form.model.content = data.content_text;
    } else {
      form.model.content.fields.forEach((e) => {
        if (!e.value) {
          e['value'] = '';
        }
        if (!e.template_field) {
          e['template_field'] = '';
        }
      });
    }
  }

  // 表单关闭时提交
  function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      delete form.model.content_text;
      let submitForm = cloneDeep(form.model);
      const { error } = await admin.notification.config(
        props.modal.params.event,
        props.modal.params.platform,
        submitForm,
      );
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }

  onMounted(() => {
    getDetail();
  });
</script>
<style lang="scss" scoped>
  .header {
    width: 100%;
    height: 40px;
    color: #434343;
    padding-left: 16px;
    background: var(--sa-background-hex-hover);
    margin: 0 0 16px 0;
    font-weight: 500;
    font-size: 14px;
  }
  .icon-warning {
    width: 14px;
    height: 14px;
    display: flex;
    margin-left: 8px;
  }
  .openswitch {
    color: var(--el-color-primary);
  }
  .switch-width {
    width: 40px;
  }
  .warning-text {
    font-size: 14px;
    line-height: 16px;
    color: var(--sa-font);
  }
  .add-richtext {
    color: var(--el-color-primary);
    font-size: 14px;
    line-height: 16px;
  }
  .question {
    color: #faad14;
    font-size: 16px;
  }
  .field-name {
    width: 100px;
    height: 32px;
  }
  .field-del {
    color: #ff4d4f;
    height: 32px;
  }
  .warning-title {
    color: rgb(255, 89, 89);
    margin-bottom: 10px;
  }
  :deep() {
    .field {
      .el-form-item__content {
        display: block;
        border: 1px solid #d9d9d9;
        max-width: 360px;
        background: var(--sa-background-hex-hover);
        border-radius: 4px;
        padding: 6px 0 2px 12px;
        color: var(--sa-place);
      }
    }
  }
</style>
